package com.mzy.basics.utils.Date;

import java.util.Calendar;
import java.util.Date;

/**
 * @author Ives
 * * @date 2021/12/27
 */
public class DateMin {
  /**
   * 获取两个时间中最小的一个时间
   * @param date
   * @param compareDate
   * @return
   */
  public static Date dateMin(Date date, Date compareDate) {
    if(date == null){
      return compareDate;
    }
    if(compareDate == null){
      return date;
    }
    if(1 == compareTime(date, compareDate)){
      return compareDate;
    }else if(-1 == compareTime(date, compareDate)){
      return date;
    }
    return date;
  }

  /**
   * 时间比较（如果myDate>compareDate返回1，<返回-1，相等返回0）
   * @param myDate 时间
   * @param compareDate 要比较的时间
   * @return int
   */
  public static int compareTime(Date myDate, Date compareDate) {
    Calendar myCal = Calendar.getInstance();
    Calendar compareCal = Calendar.getInstance();
    myCal.setTime(myDate);
    compareCal.setTime(compareDate);
    return myCal.compareTo(compareCal);
  }
}
